/* This file was generated with JastAdd2 (http://jastadd.org) version R20121112 (r872) */
package parser;

import icode.*;
import util.*;
import semanticlib.SymbolTable;
/**
 * @production Procedure : {@link Stmt} ::= <span class="component">{@link Decl}</span> <span class="component">{@link Block}</span>;
 * @ast node
 * @declaredat /h/d6/h/int12psh/workspace/CalcCode/specification/MiniP.ast:4
 */
public class Procedure extends Stmt implements Cloneable {
  /**
   * @apilevel low-level
   */
  public void flushCache() {
    super.flushCache();
  }
  /**
   * @apilevel internal
   */
  public void flushCollectionCache() {
    super.flushCollectionCache();
  }
  /**
   * @apilevel internal
   */
  @SuppressWarnings({"unchecked", "cast"})
  public Procedure clone() throws CloneNotSupportedException {
    Procedure node = (Procedure)super.clone();
    return node;
  }
  /**
   * @apilevel internal
   */
  @SuppressWarnings({"unchecked", "cast"})
  public Procedure copy() {
      try {
        Procedure node = (Procedure)clone();
        if(children != null) node.children = (ASTNode[])children.clone();
        return node;
      } catch (CloneNotSupportedException e) {
      }
      System.err.println("Error: Could not clone node of type " + getClass().getName() + "!");
      return null;
  }
  /**
   * Create a deep copy of the AST subtree at this node.
   * The copy is dangling, i.e. has no parent.
   * @return dangling copy of the subtree at this node
   * @apilevel low-level
   */
  @SuppressWarnings({"unchecked", "cast"})
  public Procedure fullCopy() {
    try {
      Procedure tree = (Procedure) clone();
      tree.setParent(null);// make dangling
      if (children != null) {
        tree.children = new ASTNode[children.length];
        for (int i = 0; i < children.length; ++i) {
          if (children[i] == null) {
            tree.children[i] = null;
          } else {
            tree.children[i] = ((ASTNode) children[i]).fullCopy();
            ((ASTNode) tree.children[i]).setParent(tree);
          }
        }
      }
      return tree;
    } catch (CloneNotSupportedException e) {
      throw new Error("Error: clone not supported for " +
        getClass().getName());
    }
  }
  /**
   * @ast method 
   * @aspect Generator
   * @declaredat /h/d6/h/int12psh/workspace/CalcCode/specification/ICodeGenerator.jadd:14
   */
  void icodeGen(){
	Code code = new Code();
	TempFactory factory = new TempFactory();

	Enter enter = new Enter(3,3);
	code.addInstruction(enter);
	//getDecl().icodeGen();
	getBlock().icodeGen(code,factory);
	enter.setTemps(factory.getNumberOfTemps());
	enter.setVars(this.totalVars);
	code.addInstruction(new Return());
	System.out.println(code);
    }
  /**
   * @ast method 
   * @aspect NameAnalysis
   * @declaredat /h/d6/h/int12psh/workspace/CalcCode/specification/NameAnalysis.jadd:23
   */
  public void analyze(SymbolTable table){

		table.enterBlock();
		getDecl().analyze(table);
		getBlock().analyze(table);
		table.exitBlock();
	}
  /**
   * @ast method 
   * @aspect VarNumbering
   * @declaredat /h/d6/h/int12psh/workspace/CalcCode/specification/VarNumbering.jadd:7
   */
  
	public int totalVars;
  /**
   * @ast method 
   * @aspect VarNumbering
   * @declaredat /h/d6/h/int12psh/workspace/CalcCode/specification/VarNumbering.jadd:21
   */
  void numbering(){
		int varCount = 0;
		for (int k=0; k<getNumChild(); k++) {
		varCount = getChild(k).numbering(varCount);
		}
		totalVars = varCount;
	}
  /**
   * @ast method 
   * 
   */
  public Procedure(int i) {
    super(i);
  }
  /**
   * @ast method 
   * 
   */
  public Procedure(MiniP p, int i) {
    this(i);
    parser = p;
  }
  /**
   * @ast method 
   * 
   */
  public Procedure() {
    this(0);


  }
  /**
   * @ast method 
   * 
   */
  public Procedure(Decl p0, Block p1) {
    setChild(p0, 0);
    setChild(p1, 1);
  }
  /**
   * @ast method 
   * 
   */
  public void dumpTree(String indent, java.io.PrintStream pStream) {
    pStream.println(indent + "Procedure");
        String childIndent = indent + "  ";
    for(int i = 0; i < getNumChild(); i++)
      getChild(i).dumpTree(childIndent, pStream);
  }
  /**
   * @ast method 
   * 
   */
  public Object jjtAccept(MiniPVisitor visitor, Object data) {
    return visitor.visit(this, data);
  }
  /**
   * @ast method 
   * 
   */
  public void jjtAddChild(Node n, int i) {
    checkChild(n, i);
    super.jjtAddChild(n, i);
}
  /**
   * @ast method 
   * 
   */
  public void checkChild(Node n, int i) {
    if(i == 0 && !(n instanceof Decl))  throw new Error("Child number 0 of Procedure has the type " + n.getClass().getName() + " which is not an instance of Decl");
    if(i == 1 && !(n instanceof Block))  throw new Error("Child number 1 of Procedure has the type " + n.getClass().getName() + " which is not an instance of Block");
  }
  /**
   * @apilevel low-level
   * @ast method 
   * 
   */
  public int getNumChild() {
    return 2;
  }
  /**
   * Replaces the Decl child.
   * @param node The new node to replace the Decl child.
   * @apilevel high-level
   * @ast method 
   * 
   */
  public void setDecl(Decl node) {
    setChild(node, 0);
  }
  /**
   * Retrieves the Decl child.
   * @return The current node used as the Decl child.
   * @apilevel high-level
   * @ast method 
   * 
   */
  public Decl getDecl() {
    return (Decl)getChild(0);
  }
  /**
   * Retrieves the Decl child.
   * <p><em>This method does not invoke AST transformations.</em></p>
   * @return The current node used as the Decl child.
   * @apilevel low-level
   * @ast method 
   * 
   */
  public Decl getDeclNoTransform() {
    return (Decl)getChildNoTransform(0);
  }
  /**
   * Replaces the Block child.
   * @param node The new node to replace the Block child.
   * @apilevel high-level
   * @ast method 
   * 
   */
  public void setBlock(Block node) {
    setChild(node, 1);
  }
  /**
   * Retrieves the Block child.
   * @return The current node used as the Block child.
   * @apilevel high-level
   * @ast method 
   * 
   */
  public Block getBlock() {
    return (Block)getChild(1);
  }
  /**
   * Retrieves the Block child.
   * <p><em>This method does not invoke AST transformations.</em></p>
   * @return The current node used as the Block child.
   * @apilevel low-level
   * @ast method 
   * 
   */
  public Block getBlockNoTransform() {
    return (Block)getChildNoTransform(1);
  }
}
